package org.javaapps.demo.dao.hibernate4;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.javaapps.common.dao.hibernate4.BaseHibernateDao;
import org.javaapps.demo.common.DateUtil;
import org.javaapps.demo.common.QueryCondition;
import org.javaapps.demo.dao.UserDao;
import org.javaapps.demo.model.UserModel;
import org.javaapps.demo.model.UserQueryModel;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

/**
 * User: Lin Jianxiong
 * Date: 11-12-26 下午4:19
 * Version: 1.0
 */
@Repository("UserDao")
public class UserHibernate4DaoImpl extends BaseHibernateDao<UserModel, Integer> implements UserDao {

    private static final String HQL_LIST = "from UserModel ";
    private static final String HQL_COUNT = "select count(*) from UserModel ";

    private static final String HQL_LIST_QUERY_CONDITION = " where username like ?";
    private static final String HQL_LIST_QUERY_ALL = HQL_LIST + HQL_LIST_QUERY_CONDITION + "order by id desc";
    private static final String HQL_COUNT_QUERY_ALL = HQL_COUNT + HQL_LIST_QUERY_CONDITION;

    @Override
    public List<UserModel> query(int pn, int pageSize, UserQueryModel command) {
        return listWithPage(HQL_LIST_QUERY_ALL, pn, pageSize, getQueryParam(command));
    }

    @Override
    public int countQuery(UserQueryModel command) {
        return this.<Number>uniqueQuery(HQL_COUNT_QUERY_ALL, getQueryParam(command)).intValue();
    }
    

    private Object[] getQueryParam(UserQueryModel command) {
        //TODO 改成全文索引
        String usernameLikeStr = "%" + command.getUsername() + "%";
        return new Object[]{
            usernameLikeStr
        };
    }

	@Override
	public UserModel getUser(UserModel model) {
		String hql = "from UserModel";
		boolean first = true;
		List<String> list = new ArrayList<String>();
		if(!StringUtils.isEmpty(model.getEmail())){
			hql += QueryCondition.getAndEqual(first, "email");
			list.add(model.getEmail());
			first = false;
		}
		if(model.getId() != null){
			hql += QueryCondition.getAndEqual(first, "id");
			first = false;
			list.add(model.getId() + "");
		}
		if(!StringUtils.isEmpty(model.getPassword())){
			hql += QueryCondition.getAndEqual(first, "password");
			first = false;
			list.add(model.getPassword());
		}
		if(!StringUtils.isEmpty(model.getUsername())){
			hql += QueryCondition.getAndEqual(first, "username");
			first = false;
			list.add(model.getUsername());
		}
		if(model.getRegisterDate() != null){
			hql += QueryCondition.getAndEqual(first, "username");
			first = false;
			list.add(DateUtil.dateFormat(model.getRegisterDate(), DateUtil.Y_M_D));
		}
		
		List<UserModel> result = listHql(hql, -1, -1, null, list.toArray());
		if(result.size() == 0)
			return null;
		return result.get(0);
	}

	@Override
	public <T> List<T> list(DetachedCriteria criteria) {
		// TODO Auto-generated method stub
		return null;
	}
}
